package com.caozhi.mspeoject.nb_search.save;

import com.caozhi.mspeoject.nb_search.remote.*;
import com.caozhi.mspeoject.nb_search.repository.FeedbackSearchRepository;
import com.caozhi.mspeoject.nb_search.repository.MessageSearchRepository;
import com.caozhi.mspeoject.nb_search.repository.OrderSearchRepository;
import com.caozhi.mspeoject.nb_search.repository.UserSearchRepository;
import com.woniuxy.boot.common.bean.*;
import com.woniuxy.boot.common.dto.FeedbackDTO;
import com.woniuxy.boot.common.dto.UserDTO;
import com.woniuxy.boot.common.util.EntityConverter;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class syncDataToElasticsearch {

    @Autowired
    private UserRemote userRemote;
    @Autowired
    private CarRemote carRemote;
    @Autowired
    private OrderRemote orderRemote;
    @Autowired
    private FeedbackRemote feedbackRemote;
    @Autowired
    private MessageRemote messageRemote;
    @Autowired
    private FeedbackSearchRepository feedbackSearchRepository;
    @Autowired
    private UserSearchRepository userSearchRepository;
    @Autowired
    private OrderSearchRepository orderSearchRepository;
    @Autowired
    private MessageSearchRepository messageSearchRepository;


    /**
     * 将反馈数据同步到ES
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void syncFeedbackDataToElasticsearch() {
        feedbackSearchRepository.deleteAll();
        List<EsFeedback> feedbackList = new ArrayList<>();
        List<FeedbackDTO> feedbackList1 = feedbackRemote.getAllFeedback();
        for (FeedbackDTO feedback : feedbackList1) {
            UserDTO userDTO = userRemote.getUserById(feedback.getUserId());
            EsFeedback esFeedback = EntityConverter.convert(feedback, EsFeedback.class);
            esFeedback.setName(userDTO.getName());
            esFeedback.setPhone(userDTO.getPhone());
            feedbackList.add(esFeedback);
        }
        feedbackSearchRepository.saveAll(feedbackList);
    }


    /**
     * 将用户数据同步到ES
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void syncUserDataToElasticsearch() {
        userSearchRepository.deleteAll();
        List<UserDTO> userList = userRemote.getAllUser();
        List<EsUser> users = new ArrayList<>();
        userList.forEach(user -> {
            EsUser esUser = EntityConverter.convert(user, EsUser.class);
            esUser.setIdCard(userRemote.getUserIdCard(user.getId()).getIdCard());
        });
        userSearchRepository.saveAll(users);
    }

    /**
     * 将订单数据同步到ES
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void syncOrderDataToElasticsearch() {
        orderSearchRepository.deleteAll();
        List<Order> orderList = orderRemote.list();
        List<EsOrder> esOrders = new ArrayList<>();
        orderList.forEach(order -> {
            UserDTO userDTO = userRemote.getUserById(order.getUserId());
            Car car = carRemote.getCarInfo(order.getCarId());
            EsOrder esOrder = EntityConverter.convert(order, EsOrder.class);
            esOrder.setName(userDTO.getName());
            esOrder.setPhone(userDTO.getPhone());
            esOrder.setCarNumber(car.getCarNumber());
            esOrder.setBrand(car.getBrand());
            esOrder.setFrameNumber(car.getFrameNumber());
            esOrders.add(esOrder);
        });
        orderSearchRepository.saveAll(esOrders);
    }


    /**
     * 将资讯数据同步到ES
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void syncMessageDataToElasticsearch() {
        messageSearchRepository.deleteAll();
        List<Message> messageList = messageRemote.getMessages();
        List<EsMessage> esMessages = new ArrayList<>();
        messageList.forEach(message -> {
            EsMessage esMessage = EntityConverter.convert(message, EsMessage.class);
            List<Media> medias = messageRemote.getMediaByMessageId(message.getId());
            if (medias == null || medias.isEmpty()) {
                esMessages.add(esMessage);
            } else {
                esMessage.setMediaList(medias);
                esMessages.add(esMessage);
            }
        });
        messageSearchRepository.saveAll(esMessages);
    }
    @PostConstruct
    public void initSyncOrderDataToElasticsearch() {
        syncOrderDataToElasticsearch();
        syncFeedbackDataToElasticsearch();
        syncMessageDataToElasticsearch();
    }
}
